﻿using System.Configuration;

namespace Nardax.Data.Configuration
{
    /// <summary>
    /// Implements a collection of the <see cref="DatabaseConnectionPolicyElement"/> configuration elements holding retry policy parameters.
    /// </summary>
    public sealed class DatabaseConnectionPolicyElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// Returns a <see cref="DatabaseConnectionPolicyElement"/> element from the collection by the specified index.
        /// </summary>
        /// <param name="idx">The item index.</param>
        /// <returns>The <see cref="DatabaseConnectionPolicyElement"/> element at the specified index.</returns>
        public DatabaseConnectionPolicyElement this[int idx]
        {
            get { return (DatabaseConnectionPolicyElement)BaseGet(idx); }
        }

        /// <summary>
        /// Adds a <see cref="DatabaseConnectionPolicyElement"/> element to the collection.
        /// </summary>
        /// <param name="element">A <see cref="DatabaseConnectionPolicyElement"/> element to add.</param>
        public void Add(DatabaseConnectionPolicyElement element)
        {
            BaseAdd(element);
        }

        /// <summary>
        ///  Returns a <see cref="DatabaseConnectionPolicyElement"/> element with the specified name.
        /// </summary>
        /// <param name="name">The name of the element to return.</param>
        /// <returns>The <see cref="DatabaseConnectionPolicyElement"/> element with the specified name; otherwise null.</returns>
        public DatabaseConnectionPolicyElement Get(string name)
        {
            return (DatabaseConnectionPolicyElement)BaseGet(name);
        }

        /// <summary>
        /// Determines whether or not a <see cref="DatabaseConnectionPolicyElement"/> element with the specified name exists in the collection.
        /// </summary>
        /// <param name="name">The name of the element to find.</param>
        /// <returns>True if the element was found, otherwise false.</returns>
        public bool Contains(string name)
        {
            return BaseGet(name) != null;
        }

        /// <summary>
        /// Creates a new instance of a configuration element which this section contains.
        /// </summary>
        /// <returns>An instance of the <see cref="DatabaseConnectionPolicyElement"/> object.</returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new DatabaseConnectionPolicyElement();
        }

        /// <summary>
        /// Gets the element key for a specified configuration element.
        /// </summary>
        /// <param name="element">The configuration element to return the key for.</param>
        /// <returns>An object that acts as the key for the specified configuration element.</returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DatabaseConnectionPolicyElement)element).Name;
        }
    }
}
